Page({
  data: {
    cardCur: 0,
    swiperList: [{
      id: 0,
      type: 'image',
      url: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1827469853,2249238395&fm=26&gp=0.jpg'
    }, {
      id: 1,
        type: 'image',
        url: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1312578375,2255303905&fm=26&gp=0.jpg',
    }, {
      id: 2,
      type: 'image',
      url: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1905003444,1227722271&fm=26&gp=0.jpg'
    }, {
      id: 3,
      type: 'image',
      url: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3493543213,2295450948&fm=26&gp=0.jpg'
    }, {
      id: 4,
      type: 'image',
      url: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2917442390,1976690903&fm=26&gp=0.jpg'
    }],
    activityList: [],
  },

  onLoad() {
    //console.log("123")
    this.towerSwiper('swiperList');
    // 初始化towerSwiper 传已有的数组名即可
    this.getActivityList();
  },

  getActivityList: function(){
    let that = this;
    wx.request({
      url: 'http://localhost:8080/activity/getActivity',
      method: 'GET',
      header: {'content-type': 'application/x-www-form-urlencoded'},
      data: {},
      success: function(res){
        console.log(res.data)
        that.setData({
          activityList: res.data.data,
        })
      }
    })
  },

  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },
  participateActivity: function(e){
    //console.log("E:" + JSON.stringify(e));
    let that = this;
    let userInfo = wx.getStorageSync('userInfo').data;
    wx.request({
      url: 'http://localhost:8080/activity/participateActivity',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        'userid': userInfo.id,
        'activityid': e.currentTarget.dataset.participate,
      },
      success: function (res) {
        // wx.setStorageSync('dynamicInfo', res.data);
          if (res.data.status == 1) {
            let newActivity = that.data.activityList;
            newActivity[e.currentTarget.dataset.curindex].participateNum += 1;
            that.setData({
              activityList: newActivity,
            });
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          } else if (res.data.status == -1){
            wx.showToast({
              title: res.data.msg,
              icon: 'none', 
              duration: 2000
            })
          } else if (res.data.status == 2){
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
      }
    })
  },

  ilike: function(e){
    let that = this;
    let userInfo = wx.getStorageSync('userInfo').data;
    wx.request({
      url: 'http://localhost:8080/activity/likeActivity',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        'userid': userInfo.id,
        'activityid': e.currentTarget.dataset.participate,
      },
      success: function (res) {
        // wx.setStorageSync('dynamicInfo', res.data);
          if (res.data.status == 1) {
            let newActivity = that.data.activityList;
            newActivity[e.currentTarget.dataset.curindex].likes += 1;
            that.setData({
              activityList: newActivity,
            });
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
      }
    })
  },
  lookActivity: function(e){
    wx.setStorageSync('activityInfo', this.data.activityList[e.currentTarget.dataset.curindex])
    wx.navigateTo({
      url: '/pages/home/activity/activity',
    })
  },
})